﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;
using ForkCarRelationManager.Utility;
using common;
using ForkCarRelationManager.DB;
namespace ForkCarRelationManager.Controls
{
    public partial class ClientControl : UserControl
    {
        DataPaging ClientPage;
        DataSet ClientDataSet;
        Util util = new Util();
        int pagecount = 18;
        //初始化业务操作类
        ClientBI clientBI = new ClientBI();

        public ClientControl()
        {
            InitializeComponent();
        }
        /*
         * 加载控件初始化
         * */
        private void ClientControl_Load(object sender, EventArgs e)
        {

            if (this.button1.Image != null)
            {
                button1.Image = new Bitmap(button1.Image, button1.Height - 4, button1.Height - 4);
            }
            //初始化下拉列表
            this.initListCompany();

            //初始化分页数据
            ClientPage = clientBI.GetClientPage(pagecount);
            InitData();
            this.dataGridView1.ReadOnly = true;
        }

        private void InitData()
        {
          //  ClientPage = clientBI.GetClientPage(pagecount);
            ClientDataSet = ClientPage.Homepage();
            int x = clientBI.GetDatacount(ClientPage);
            labTool.Text = "总共" + x + "条记录，当前第" + ClientPage.GetPage() + "页，每页" + pagecount + "条";
            this.dataGridView1.DataSource = ClientDataSet;
            this.dataGridView1.DataMember = "data";
        }

        /*
         * 初始化数据
         * */
        private void initListCompany()
        {
            //初始化下拉列表
            DataTable companyTable = clientBI.initCompany();
            DataRow maintordr = companyTable.NewRow();
            maintordr[0] = "0";
            maintordr[1] = "";
            companyTable.Rows.InsertAt(maintordr, 0);
            TBCompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBCompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBCompany.DataSource = companyTable;
            this.TBCompany.DisplayMember = "ScompanyName";
            this.TBCompany.ValueMember = "ScompanyName";

            //初始化客户列表
           // this.dataGridView1.DataSource = clientBI.initClient();
           
        }

        /*
         * 
         * 按照客户姓名，联系人，区域模糊搜索
         * */
        private void button1_Click(object sender, EventArgs e)
        {
            TBCompany.Text = "";
            //this.dataGridView1.DataSource = clientBI.searchWithStr(TBSearch.Text);
            ClientPage = clientBI.searchWithStr(TBSearch.Text, pagecount);
            InitData();
        }

        private void labelX7_Click(object sender, EventArgs e)
        {

        }
        /*
        * 点击添加按钮
        * */
        private void BtnAddClient_Click_1(object sender, EventArgs e)
        {
            FormAddClient formClient = new FormAddClient();
            formClient.ShowDialog();
            if (formClient.DialogResult == DialogResult.Yes)
            {
                ClientPage = clientBI.GetClientPage(pagecount);
                this.InitData();
            }
        }

        /*
         * 选择分公司（只有当鼠标点击的时候触发，初始化的时候不会执行）
         * 此搜索会包括模糊查询的条件
         * */
        private void TBCompany_SelectionChangeCommitted(object sender, EventArgs e)
        {
           //确定时间
            if (RBAll.Checked)
            {
                ClientPage = clientBI.searchWithCompany(this.TBCompany.SelectedValue.ToString(), TBSearch.Text, pagecount);
                InitData();
            }
            if (RBThreeDays.Checked)
            {
                threeDays();
            }
            if (RBWeek.Checked)
            {
                aweek();
            }
            if (RBMonth.Checked)
            {
                amonth();
            }
            if (RBDIY.Checked)
            {
                dateTimePicker2_ValueChanged(null, null);
            }
        }

        /*
         *按照最近三天维修记录查询 
         */
        private void threeDays()
        {
            DateTime endTime = DateTime.Now.Date;
            DateTime beginTime= DateTime.Now.AddDays(-3).Date;
            ClientPage = clientBI.searchWithCompany(this.TBCompany.SelectedValue.ToString(), this.TBSearch.Text, beginTime, endTime, pagecount);
            InitData();
        }

        /*
         * 
         * 按照最近七天维修记录查询
         * */
        private void aweek()
        {
            DateTime endTime = DateTime.Now.Date;
            DateTime beginTime = DateTime.Now.AddDays(-7).Date;
            ClientPage = clientBI.searchWithCompany(this.TBCompany.SelectedValue.ToString(), this.TBSearch.Text, beginTime, endTime, pagecount);
            InitData();
        }
        /*
        * 
        * 按照最近30天维修记录查询
        * */
        private void amonth()
        {
            DateTime endTime = DateTime.Now.Date;
            DateTime beginTime = DateTime.Now.AddDays(-30).Date;
             ClientPage = clientBI.searchWithCompany(this.TBCompany.SelectedValue.ToString(), this.TBSearch.Text, beginTime, endTime, pagecount);
             InitData();
        }

        /*
       * 按照起止时间查询
       * */
        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            DateTime beginTime = this.dateTimePicker1.Value.Date;
            DateTime endTime = this.dateTimePicker2.Value.Date;
            if (beginTime <= endTime)
            {
                ClientPage = clientBI.searchWithCompany(this.TBCompany.SelectedValue.ToString(), this.TBSearch.Text, beginTime, endTime, pagecount);
                InitData();
            }
            else
            {
                MessageBox.Show("请重新选择时间段！");
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.ColumnIndex>=0)
            {
                string s = dataGridView1.CurrentRow.Cells[dataGridView1.CurrentRow.Cells.Count-1].Value.ToString();
                FormClientDetail detial = new FormClientDetail();
                detial.clientId = s;
                detial.ShowDialog();
                if (detial.DialogResult == DialogResult.Yes)
                {
                    InitData();
                }
            }
        }
        //首页
        private void btn_mainpage_Click(object sender, EventArgs e)
        {
            InitData();
        }
        /*
        * 上一页
        * */
        private void btn_lastpage_Click(object sender, EventArgs e)
        {
            if (ClientPage.GetPage() > 1)
            {
                ClientDataSet = ClientPage.GetForeData();
                 this.dataGridView1.DataSource = ClientDataSet;

                 labTool.Text = "总共" + clientBI.GetDatacount(ClientPage) + "条记录，当前第" + ClientPage.GetPage() + "页，每页" + pagecount + "条";
                 this.dataGridView1.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是第一页");
            }
        }
        /*
         * 下一页
         * */
        private void btn_nextpage_Click(object sender, EventArgs e)
        {
            if (ClientPage.GetPage() < ClientPage.GetTotalCount())
            {
                ClientDataSet = ClientPage.GetFollowData();
                this.dataGridView1.DataSource = ClientDataSet;
                labTool.Text = "总共" + clientBI.GetDatacount(ClientPage) + "条记录，当前第" + ClientPage.GetPage() + "页，每页" + pagecount + "条";
                this.dataGridView1.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是最后一页");
            }
        }
        /*
         * 末页
         * */
        private void btn_endpage_Click(object sender, EventArgs e)
        {
            if (ClientPage.GetTotalCount() == 1)
            {
                InitData();
            }
            else
            {
                ClientDataSet = ClientPage.TailPage();
                if (ClientDataSet != null && ClientDataSet.Tables["data"].Rows.Count > 0)
                {
                     this.dataGridView1.DataSource = ClientDataSet;
                     labTool.Text = "总共" + clientBI.GetDatacount(ClientPage) + "条记录，当前第" + ClientPage.GetPage() + "页，每页" + pagecount + "条";
                     this.dataGridView1.DataMember = "data";
                }
            }
        }
        /*
         * 跳页
         * */
        private void btn_skippage_Click(object sender, EventArgs e)
        {
            if (TBPageNum.Text.Trim().Length!=0 && util.isnumeric(TBPageNum.Text.Trim()))
            {
                int skippage = Convert.ToInt32(TBPageNum.Text.Trim());
                if (skippage == 1)
                {
                    InitData();
                }
                else
                {
                    ClientDataSet = ClientPage.SkipPage(skippage);
                    if (ClientDataSet != null && ClientDataSet.Tables["data"].Rows.Count > 0)
                    {
                         this.dataGridView1.DataSource = ClientDataSet;
                         labTool.Text = "总共" + clientBI.GetDatacount(ClientPage) + "条记录，当前第" + ClientPage.GetPage() + "页，每页" + pagecount + "条";
                         this.dataGridView1.DataMember = "data";
                    }
                }
            }
            else
                MessageBox.Show("请输入数字再进行跳页");
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);  
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(this.dataGridView1, ClientPage.allData(), 1);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
          
            
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        //选择按钮
        private void RBDIY_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton thisRadioBtn = (RadioButton)sender;
            int tag = Convert.ToInt32(thisRadioBtn.Tag.ToString());
            switch (tag)
            {
                case 1:
                    //点击全部按钮
                    if (thisRadioBtn.Checked)
                    {
                        ClientPage = clientBI.searchWithCompany(this.TBCompany.SelectedValue.ToString(), TBSearch.Text, pagecount);
                        InitData();
                    }
                    break;

                case 2:
                    //点击近三天
                    if (thisRadioBtn.Checked)
                    {
                        threeDays();
                    }
                    break;

                case 3:
                    //点击近一周
                    if (thisRadioBtn.Checked)
                    {
                        aweek();
                    }
                    break;

                case 4:
                    //点击近一月
                    if (thisRadioBtn.Checked)
                    {
                        amonth();
                    }
                    break;
                  
                case 5:
                    //点击自定义
                    if (thisRadioBtn.Checked)
                    {
                        this.dateTimePicker1.Enabled = true;
                        this.dateTimePicker2.Enabled = true;

                    }
                    else
                    {
                        this.dateTimePicker1.Enabled = false;
                        this.dateTimePicker2.Enabled = false;
                    }
                    break;
            }
            
        }
      

    }
}
